function [ gradW ] = gradGaussianWeight( xNodes, xi, rho )
%GRADGAUSSIANWEIGHT Computes the spatial gradient of the Gaussian Weight
%                   function, and evaluates it on th given point x.
%
%   The patial gradient is a vector for each xSamples sptaial point,
%   then it is a matrix, where first dimension is the sample point, and
%   second dimension the spatial coordinates for the derivative evaluated
%   at the sample point.
%
%   xNodes   : Nodes array, first dimension is the point, second
%              dimension the spatial coordinates.
%   xi       : A sample point.
%   rho      : Spacing parameter.

dimension = size(xi,2);
nPts  = size(xNodes, 1);
gradW = zeros(nPts, dimension);
diff  = gradW;
diff2 = zeros(nPts,1);

for i=1:nPts
diff(i, :) =  xi - xNodes(i,:);
df2 = norm (diff(i,:));
diff2(i) = df2 * df2;
end



sqrRho = rho^2;
coeff = (2/sqrRho);

for i = 1:nPts
    gradW(i,:) = -coeff * diff(i,:) *...
                            exp(-diff2(i) / sqrRho);
end
end

